﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Reflection;

public partial class controls_DataPager : System.Web.UI.UserControl
{
    private string _bindMethodName;
    private MethodInfo _bindMethod;
    private string _totalCountPropertyName;
    private int _totalPage;
    private int _currentPage;
    private string _currentRowPropertyName;
    private int _pageSize;
    private int _totalItem;

    public int PageSize
    {
        get { return this._pageSize; }
        set { this._pageSize = value; }
    }

    public string CurrentRowPropertyName
    {
        get { return this._currentRowPropertyName; }
        set { this._currentRowPropertyName = value; }
    }

    public int CurrentRow
    {
        get
        {
            return (int)Page.GetType().GetProperty(_currentRowPropertyName,
                typeof(int)).GetGetMethod().Invoke(this, null);
        }
        set
        {
            Page.GetType().GetProperty(_currentRowPropertyName,
                typeof(int)).GetSetMethod().Invoke(this, new object[]{value});
        }
    }

    public string TotalCountPropertyName
    {
        get { return this._totalCountPropertyName; }
        set { this._totalCountPropertyName = value; }
    }

    public string BindMethodName
    {
        get { return this._bindMethodName; }
        set { this._bindMethodName = value; }
    }

    public MethodInfo BindMethod
    {
        get
        {
            if (_bindMethod == null)
            {
                _bindMethod = Page.GetType().GetMethod(_bindMethodName);
            }
            return _bindMethod;
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        PropertyInfo totalItemsProperty = Page.GetType().GetProperty(_totalCountPropertyName);
        MethodInfo totalItemMethod = totalItemsProperty.GetGetMethod();
        object o = totalItemMethod.Invoke(sender, null);
  
        _totalPage = _totalItem / (_pageSize + 1);
    }

    private void CallBindMethod()
    {
        BindMethod.Invoke(this, null);
    }

    protected void FirstPage_Click(object sender, EventArgs e)
    {
        CurrentRow = 0;
        _currentPage = 1;

        CallBindMethod();

        pageStatus.Text = "Page 1 of " + _totalPage.ToString();
    }

    protected void PrevPage_Click(object sender, EventArgs e)
    {
        CurrentRow -= _pageSize;
        if (CurrentRow < 0)
            CurrentRow = 0;

        _currentPage = CurrentRow / _pageSize;

        CallBindMethod();
        pageStatus.Text = "Page " + _currentPage.ToString() + " of " + _totalPage.ToString();
    }

    protected void NextPage_Click(object sender, EventArgs e)
    {
        CurrentRow += _pageSize;
        int itemCount = _totalItem;

        if (CurrentRow >= _totalItem)
        {
            int mod = _totalItem % (_pageSize);

            if (mod == 0 && _totalItem != 0)
                CurrentRow = _totalItem - _pageSize;
            else
                CurrentRow = _totalItem - mod;

            if (CurrentRow < 0)
                CurrentRow = 0;
        }
        _currentPage = CurrentRow / _pageSize;

        CallBindMethod();
        pageStatus.Text = "Page " + _currentPage.ToString() + " of " + _totalPage.ToString();
    }

    protected void LastPage_Click(object sender, EventArgs e)
    {
        int mod = _totalItem % (_pageSize);

        if (mod == 0 && _totalItem != 0)
            CurrentRow = _totalItem - _pageSize;
        else
            CurrentRow = _totalItem - mod;

        if (CurrentRow < 0)
            CurrentRow = 0;

        _currentPage = CurrentRow / _pageSize;

        CallBindMethod();
        pageStatus.Text = "Page "+ _totalPage.ToString() +" of " + _totalPage.ToString();
    }

    protected void MidPage_Click(object sender, EventArgs e)
    {
        CurrentRow = _currentPage * _pageSize;
        _currentPage = _totalPage;
        CallBindMethod();
    }
}
